var url_href = "https://collect.topeffects.cn/api_activity_info?activity_id=93";
var url = 'https://collect.topeffects.cn/api_activity_info';
var data = {
	activity_id: 93
};


var arr_imgurl = [];
var arr_name = [];
var arr_title = [];
var arr_subtitle = [];
var coupon_id = [];
var link_url = [];
var now_url=link_url;;
console.log(link_url);
window.onload = function () {
	$.get(url, data, function (e) {
		for (var i in e.coupon_list) {
			console.log(e.coupon_list[i]);
			arr_imgurl.push(e.coupon_list[i].imgurl);
			arr_name.push(e.coupon_list[i].name);
			arr_title.push(e.coupon_list[i].title);
			arr_subtitle.push(e.coupon_list[i].subtitle);
			coupon_id.push(e.coupon_list[i].id);
			link_url.push(e.coupon_list[i].link);
		}

		for (var k = 0; k < arr_imgurl.length; k++) {
			var new_box = "<div class='box'>" +
				"<div class='box_pic'>" +
				"<img class='new_img'/>" +
				"</div>" +
				"<div class='box_word'>" +
				"<p class='box_name'>" + arr_name[k] + "</p>" +
				"<p class='box_title'>" + arr_title[k] + "</p>" +
				"<p class='box_subtitle'>" + arr_subtitle[k] + "</p>" +
				"</div>" +
				"</div>"
			$(".index_pull").append(new_box);
			$('.index_pull_abos .index_pull img').eq(k).attr('src', 'https://collect.topeffects.cn/' + arr_imgurl[k]);
			$('.bottom_r').attr('href', link_url[k]);
		}
		
		var is_run = false;
		var box = $('.box').eq(0);
		var box1 = $('.box')[0];
		var detail_pub = $('.detail_pub').eq(0);
		var detail_pub1 = $('.detail_pub')[0];
		var click_x, click_y, move_x, move_y, now_x, now_y;
		var away = parseInt(window.screen.width) * 0.3;
		var p_lis = document.getElementsByClassName('p_lis');
		var roll_abs = document.getElementsByClassName('roll_abs')[0];


		function z_index() {
			for (var m = 0; m < $('.box').length; m++) {
				$('.box').eq(m).css('z-index', (parseInt($('.box').length) + 100 - m));
				if (m <= 2) {
					$('.box').eq(m).css('transform', 'scale(' + (1 - m * 0.03) + ') translateY(' + 10 * m + 'px)');
				} else {
					$('.box').eq(m).css('transform', 'scale(0.94) translateY(20px)');
				}
			}
		}
		z_index();


		//点击奖品跳至奖品页面
		$('.prize').click(function () {
			$('.prize_page').css("display", "block");
			$('.active_page').css("display", "none");
		})
		//关闭奖品页面，跳至活动页面
		$('.prize_back').click(function () {
			$('.prize_page').css("display", "none");
			$('.active_page').css("display", "block");
		})
		//点击活动说明跳至活动说明页面
		$('.active_description').click(function () {
			$('.description_page').css("display", "block");
			$('.active_page').css("display", "none");
		})
		// 活动说明页面跳至活动页面
		$('.des_back').click(function () {
			$('.description_page').css("display", "none");
			$('.active_page').css("display", "block");
		})
		// 点击卡片跳至详情页面
		$('.box').on("click", function () {
			$('.detail_page').css("display", "block");
			$('.detail_pub').eq(0).css('display', 'block');

			console.log(data.activity_id);
			console.log(coupon_id[0]);

			function mess_back(act_id, coupon_id) {
				$.ajax({
					type: "get",
					url: "https://collect.topeffects.cn/api_coupon_detail",
					data: {
						'activity_id': act_id,
						'cac_id': coupon_id
					},
					dataType: "json",

					success: function (data) {
						console.log('请求成功');
						console.log(data);
						for (var y in data.coupon_img_list) {
							var new_img = '<div class="detail_img big swiper-slide">' +
								'<img id="detail_img" src="' + "https://collect.topeffects.cn/" + data.coupon_img_list[y].caci_imgurl + '" />' +
								'</div>';
							// console.log(data.coupon_img_list[y].caci_imgurl);
							$('.swiper-wrapper').append(new_img);
						}
						$('.score').text(data.coupon_side.cacs_scoring);
						for (i = 0; i < Math.round($('.score').text()); i++) {
							$('.detail_con ul li').eq(i).css("display", "block");
						}
						$('.size').text(data.coupon_side.cacs_app_size);
						$('.introduce').text(data.coupon_side.cacs_desc);
					},
					error: function (msg) {
						console.log(msg);
					}
				})
			}
			mess_back(data.activity_id, coupon_id[0]);

			//	$('.active_page').css("display", "none");




		})
		// 详情页跳至活动页面
		$('.detail_img').click(function () {
			$('.detail_page').css("display", "none");
			$('.active_page').css("display", "block");
		})
		// 详情页面点击更多，显示介绍全部内容
		$('.more').click(function () {
			$(".detail_con p").css("display", "block");
			$(".more").text(" ");
		})

		$('.unlike').click(function () {
			$('.active_page').css('display', 'block');
			$('.detail_page').css('display', 'none');
			var delete_time = setTimeout(function () {
				(function () {
					box.css('transition', '1s')
						.css('transform', 'translate(-180%,50%) rotate(9deg)');
					$('.box').eq(1).css('transform', 'scale(1)')
						.css('transition', 'transform 0.5s linear');
					var bottom_l_time = setTimeout(function () {
						box.remove();
						z_index();
						coupon_id.shift(0);
						box1 = $('.box')[0];
						box = $('.box').eq(0);
						// detail_pub.remove();
						// detail_pub1 = $('.detail_pub')[0];
						// detail_pub = $('.detail_pub').eq(0);

						clearTimeout(bottom_l_time);
					}, 600);
					coupon_id.shift(0);
					link_url.shift(0);
					console.log(link_url[0]);
				})();
				clearTimeout(delete_time);
			}, 500);
		});

		$('.like').click(function () {
			// $('.active_page').css('display', 'block');
			// $('.detail_page').css('display', 'none');
			var delete_time = setTimeout(function () {
				window.location=now_url[0];
				// (function () {
				// 	box.css('transition', '1s')
				// 		.css('transform', 'translate(130%,50%) rotate(-9deg)');
				// 	$('.box').eq(1).css('transform', 'scale(1)')
				// 		.css('transition', 'transform 0.5s linear');
				// 	var bottom_r_time = setTimeout(function () {
				// 		box.remove();
				// 		z_index();
				// 		coupon_id.shift(0);
				// 		box1 = $('.box')[0];
				// 		box = $('.box').eq(0);
				// 		// detail_pub.remove();
				// 		// detail_pub1 = $('.detail_pub')[0];
				// 		// detail_pub = $('.detail_pub').eq(0);]
				// 		window.location=link_url[0];
				// 		clearTimeout(bottom_r_time);
				// 	}, 600);
				// 	coupon_id.shift(0);
				// })();
				// clearTimeout(delete_time);
			}, 500);
		});

		//控制高度
		var innerHeight = window.innerHeight,
			bodyHeight = document.getElementsByClassName("web")[0],
			clientHeight = document.getElementsByTagName("html")[0].clienltHeight;
		if (innerHeight < clientHeight) {
			console.log(innerHeight);
			console.log(document.getElementsByTagName("html"));
			bodyHeight.setAttribute("style", "height:" + clientHeight + "px");
		}

		$('.index_pull').on("touchstart", function (e) {
			is_run = true;
			/*点下时，允许鼠标移动，并获取坐标*/
			click_x = e.changedTouches[0].clientX;
			click_y = e.changedTouches[0].clientY;

			box1.style.willChange = 'transform';
			box.css('transition', 'all 0s');

			$(".web").on('touchmove', function (e) {
				e.preventDefault();
			});
		});
		document.addEventListener("touchmove", function (e) {
			if (is_run) {
				/*获取移动时的坐标*/
				move_x = e.changedTouches[0].clientX;
				move_y = e.changedTouches[0].clientY;
				/*相减*/
				now_x = move_x - click_x;
				now_y = move_y - click_y;

				box1.style.left = now_x + 'px';
				box1.style.top = now_y + 'px';
				$('.box').eq(1).css('transform', 'scale(1)')
					.css('transition', 'transform 0.5s linear');
				if (now_x > 0) {
					box1.style.transform = 'translate3d(0,0,0) rotate(-3deg)';
				} else if (now_x < 0) {
					box1.style.transform = 'translate3d(0,0,0) rotate(3deg)';
				}
			}
		});
		document.addEventListener("touchend", function (e) {
			is_run = false;
			box1.style.willChange = 'auto';

			$(".web").unbind("touchmove");

			if (parseInt(box.css('left')) < -away) {
				box.remove();
				z_index();
				box1 = $('.box')[0];
				box = $('.box').eq(0);
				// detail_pub.remove();
				// detail_pub1 = $('.detail_pub')[0];
				// detail_pub = $('.detail_pub').eq(0);
				link_url.shift(0);
				coupon_id.shift(0);
			} else if (parseInt(box.css('left')) > away) {
				box.remove();
				z_index();
				box1 = $('.box')[0];
				box = $('.box').eq(0);
				// detail_pub.remove();
				// detail_pub1 = $('.detail_pub')[0];
				// detail_pub = $('.detail_pub').eq(0);
				window.location=now_url[0];
				coupon_id.shift(0);
			} else {
				box.css('transition', '0.26s linear');
				box1.style.transform = 'translate3d(0,0,0) rotate(0deg)';
				box1.style.top = 0 + 'px';
				box1.style.left = 0 + 'px';
				$('.box').eq(1).css('transform', 'scale(0.97) translate3d(0,10px,0)');
			}
			if ($(".box").length == 0) {
				alert("到底了");
			}
		});
		/**左**/
		function click_l() {
			$('.bottom_l').click(function () {
				box.css('transition', '1s')
					.css('transform', 'translate(-180%,50%) rotate(9deg)');
				$('.box').eq(1).css('transform', 'scale(1)')
					.css('transition', 'transform 0.5s linear');
				var bottom_l_time = setTimeout(function () {
					box.remove();
					z_index();
					box1 = $('.box')[0];
					box = $('.box').eq(0);
					// detail_pub.remove();
					// detail_pub1 = $('.detail_pub')[0];
					// detail_pub = $('.detail_pub').eq(0);
					var now_
					link_url.shift(0);
					console.log(link_url);
					clearTimeout(bottom_l_time);
				}, 600);

				coupon_id.shift(0);
				if ($(".box").length == 0) {
					alert("到底了");
				}
			});
		}
		click_l();
		/**左**/
		/**右**/
		function click_r() {
			$('.bottom_r').click(function () {
				box.css('transition', '1s')
					.css('transform', 'translate(130%,50%) rotate(-9deg)');
				$('.box').eq(1).css('transform', 'scale(1)')
					.css('transition', 'transform 0.5s linear');
				var bottom_r_time = setTimeout(function () {
					box.remove();
					z_index();
					box1 = $('.box')[0];
					box = $('.box').eq(0);
					// detail_pub.remove();
					// detail_pub1 = $('.detail_pub')[0];
					// detail_pub = $('.detail_pub').eq(0);
					window.location=now_url[0];
					clearTimeout(bottom_r_time);
				}, 600);
				coupon_id.shift(0);
				if ($(".box").length == 0) {
					alert("到底了");
				}

			});

		}
		click_r();
		/**右**/
		for (i = 0; i < Math.round($('.score').text()); i++) {
			$('.detail_con ul li').eq(i).css("display", "block");
		}










	}, 'json');
}